#pragma once
#include "GObject.h"

GObject::GObject(void)
{
	this->_x = 0;
	this->_y = 0;
	/*this->_width = 0;
	this->_height = 0;*/
	this->_sprite = NULL;
	this->_isMovable = false;
}

GObject::GObject(float x, float y, int Width, int Height)
{
	_x = x;
	_y = y;
	/*_width = Width;
	_height = Height;*/
	_isMovable = false;

}

GObject::GObject(const GObject &object)
{
	_x = object._x;
	_y = object._y;
	_sprite = object._sprite;
	_isMovable = object._isMovable;
	this->_rm = NULL;

}

GObject::GObject(float x,float y, int resourceName, CResourceManager *rm)
{
	_x = x;
	_y = y;
	_rm = rm;
	_sprite = _rm->getSprite(resourceName);


	_isMovable = false;
}

GObject::~GObject(void)
{
	if (_sprite)
		delete _sprite;
}

void GObject::Render()
{
	float sx = 0;
	float sy = 0;
	CScreen::getInstance()->getPosition(sx,sy);

	this->_sprite->Render(_x - sx,_y - sy);
}

void GObject::Translate(Vector v)
{

	if (abs(v.getX()) >= 2.0f)
		_x += v.getX();



	if (abs(v.getY()) >= 2.f)
		_y += v.getY();

	_lastTranslateVector = v;

	actAfterCollided(getTranslateDirection(_lastTranslateVector));
}

//void GObject::Translate(Vector v, int k)
//{
//
//	if (abs(v.getX()) >= 2.0f)
//		_x += v.getX();
//
//
//
//	if (abs(v.getY()) >= 2.f)
//		_y += v.getY();
//
//	_lastTranslateVector = v;
//
//	actAfterCollided(getTranslateDirection(_lastTranslateVector),k);
//}

void GObject::setStepPercent(float percent)
{
	_stepPercent = percent;
}

RECT GObject::getRect()
{
	int width,height;
	_sprite->getSize(width,height);

	RECT rec;
	rec.left = _x;
	rec.top = _y;
	rec.right = rec.left + width;
	rec.bottom = rec.top + height;

	return rec;
}

Vector GObject::getPosition()
{
	return Vector(_x,_y);
}

bool GObject::isMovable()
{
	return _isMovable;
}

CPolygon GObject::getPolygon()
{
	return CPolygon(this->getRect());
}

void GObject::setSprite(const Sprite &sprite)
{
	_sprite = new Sprite(sprite);
}
//QuadNode* GObject::getHolder()
//{
//	return _holder;
//}
//
//void GObject::setHolder(QuadNode* holder)
//{
//	_holder = holder;
//}

int GObject::getTranslateDirection(Vector translateVector)
{
	int x = translateVector.getX();
	int y = translateVector.getY();

	if (x == 0)
	{
		if (y<0)
			return 1;		//UP
		else if (y > 0)
			return 2;		//DOWN
	}
	else
	{
		if (y==0)
		{
			if (x<0)
				return 3;	//RIGHT
			else return 4;	//LEFT
		}
	}
	return 0;
}